// pages/user/whiteList/whiteList.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pickerIndex: 0,
        pickerArray: ['购买商品白名单', '购买配额白名单'],
        id: '',
        list: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.setData({
            list: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getList()
    },

    getList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let list = this.data.list
        let data = {
            limit: 10,
            page: this.data.nowPage
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('White/white_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    list: list,
                    isAll: true
                })
            } else {
                list = list.concat(res)
                that.setData({
                    list: list,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    idInput: function(e) {
        this.setData({
            id: e.detail.value
        })
    },

    bindPickerChange: function(e) {
        this.setData({
            pickerIndex: e.detail.value
        })
    },

    addWhiteList: function() {
        if (this.data.id == '') {
            wx.showToast({
                title: '请输入需要添加白名单的ID',
                icon: 'none',
                duration: 2000
            })
            return
        }
        wx.showLoading({
            title: '请稍后'
        })
        const that = this
        let data = {
            user_id: this.data.id,
            type: this.data.pickerIndex + 1
        }
        common.post('White/add_white', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) {}
                })
                return
            }
            wx.showModal({
                title: '提示',
                content: '成功添加白名单',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            id: '',
                            list: [],
                            nowPage: 0,
                            isLoading: false,
                            isAll: false
                        })
                        that.getList()
                    }
                }
            })
        }).catch(function(e) {})
    },

    deleteWhiteList: function(e) {
        const that = this
        wx.showModal({
            title: '提示',
            content: '确认删除该白名单？',
            success(res) {
                if (res.confirm) {
                    wx.showLoading({
                        title: '请稍后'
                    })
                    let data = {
                        wilte_id: e.currentTarget.dataset.id
                    }
                    common.post('White/white_del', data).then(function(res) {
                        wx.hideLoading()
                        if (res.code == 204) {
                            wx.showModal({
                                title: '提示',
                                content: res.error,
                                showCancel: false,
                                success(res) {}
                            })
                            return
                        }
                        wx.showModal({
                            title: '提示',
                            content: '成功删除',
                            showCancel: false,
                            success(res) {
                                if (res.confirm) {
                                    that.setData({
                                        list: [],
                                        nowPage: 0,
                                        isLoading: false,
                                        isAll: false
                                    })
                                    that.getList()
                                }
                            }
                        })
                    }).catch(function(e) {})
                }
            }
        })
    }
})